package com.itheima.hotel.service.impl;

import com.itheima.hotel.data.HotelData;
import com.itheima.hotel.data.RoomsData;
import com.itheima.hotel.service.HotelMgtSystem;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

/*
业务层
 */
public class HotelMgtSystemImpl3 implements HotelMgtSystem {
    HotelData hotelData = new HotelData();
    private List<String> orderDataList = new ArrayList<>();
    private List<RoomsData> roomsDataList = hotelData.getRoomsDataList();

    /*
    订房
     */
    @Override
    public void reserveRoom() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入预定的房间号");
        String num = scanner.next();
        boolean judgeRoom = judgeRoom(num, roomsDataList);
        if (judgeRoom != true) {
            return;
        }
        //属于的时间合法化
        String time = judgeTime();

        for (RoomsData roomsData : roomsDataList) {


            if (roomsData.isReserved() != true) {
                if (roomsData.getRoomNumber().equals(num)) {
                    roomsData.setReserved(true);
                    String oderNum = getOderNum();
                    //  LocalDateTime orderNum = (LocalDateTime) oderNum;
                    String orderRoom = "预定的房间信息为： " + roomsData.toString() + " ;入住时间为: " + time.toString() + ",订单编号为：" + oderNum;
                    orderDataList.add(orderRoom);
                    System.out.println("预定成功");
                    return;
                }
            }
        }

        System.out.println("该房间已被预定，请重新选择");

    }


    /*
       退房
        */
    @Override
    public void checkOut() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入订单编号");
        String num = scanner.next();


        for (int i = 0; i < orderDataList.size(); i++) {
            //   System.out.println("从预定房间内删除信息");
            boolean contains = orderDataList.get(i).contains(num);
            for (RoomsData roomsData : roomsDataList) {
                if (roomsData.isReserved() != false) {
                    if (orderDataList.get(i).contains(roomsData.getRoomNumber()) == true) {
                        roomsData.setReserved(false);

                    }
                }
                //   System.out.println(contains);
                if (contains == true) {
                    orderDataList.remove(i);
                    System.out.println("退房成功");
                    return;
                }

            }
        }

        System.out.println("当前订单号不存在");
    }


    /*
    查询所有房间信息
     */
    @Override
    public void printAllRooms() {
        // List<RoomsData> roomsDataList = hotelData.getRoomsDataList();
     /*   Collections.sort(roomsDataList,(s1,s2)->{
            (int )(s1.getRoomNumber().compareTo()-s2.getRoomNumber());
        });*/
        for (RoomsData roomsData : roomsDataList) {
            System.out.println(roomsData.toString());
            System.out.println();
        }

    }

    /*
    查询预定房间信息
     */
    @Override
    public void printOderRooms() {
        if (orderDataList == null) {
            System.out.println("还未预定房间");
            return;
        } else {
            for (String oderRoom : orderDataList) {
                System.out.println(oderRoom.toString());
            }
        }


    }

    /*
    获取当前时间信息
     */
    public static LocalDateTime getTime() {
        LocalDateTime localDateTime = LocalDateTime.now();
        //  System.out.println("当前时间为 " + localDateTime);
        return localDateTime;

    }

    /*
    生成订单号
     */
    public String getOderNum() {
        Random r = new Random();
        while (true) {
            String oderNum = "";
            for (int i = 0; i < 6; i++) {
                int num = r.nextInt(10);
                oderNum += num;
            }


            boolean contains = orderDataList.contains(oderNum);
            if (contains != true) {
                return oderNum;
            }
        }

    }


    //判断输入的时间是否合法
    public static String judgeTime() {
        while (true) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入预定日期;\"时间格式为:2016-12-31 \"");
            String starTime = scanner.next();
            //String regex = "^[1-9]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\\s+(20|21|22|23|[0-1]\\d):[0-5]\\d";
            String regex = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|" +
                    "((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|" +
                    "((0[48]|[2468][048]|[3579][26])00))-02-29)$";

            boolean matches = starTime.matches(regex);
            if (matches == true) {

                return starTime;

            } else {
                System.out.println("您输入的入住日期不合法");
            }
        }

    }//方法

    //判断房间是否存在
    public static boolean judgeRoom(String num, List<RoomsData> roomsDataList) {

        for (RoomsData roomsData : roomsDataList) {
            if (roomsData.getRoomNumber().equals(num) == true) {
                return true;
            }
        }

        System.out.println("该房间不存在");
        return false;
    }
}
